package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenRelatedCaseDO;
import com.ruoyi.project.men.service.MenRelatedCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目案例Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menRelatedCase")
public class MenRelatedCaseController extends BaseController
{
	@Autowired
	private MenRelatedCaseService menRelatedCaseService;


	/*@GetMapping("/{id}")
	@RequiresPermissions("men:menRelatedCase:menRelatedCase")
	String MenRelatedCase(@PathVariable("id") String id, Model model){
		if(StrUtil.isNotBlank(id)){
			String[] split = id.split("&");
			model.addAttribute("parentId",split[0]);
			model.addAttribute("status",split[1]);

		}
		return "men/menRelatedCase/menRelatedCase";
	}*/

	/**
	 * 查询项目案例列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenRelatedCaseDO menRelatedCase)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menRelatedCase);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		stringObjectMap.put("status",menRelatedCase.getStatus()+"");
		List<MenRelatedCaseDO> list = menRelatedCaseService.likeList(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出项目案例列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:export')")
	@Log(title = "项目案例", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenRelatedCaseDO menRelatedCase)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menRelatedCase);
		List<MenRelatedCaseDO> list = menRelatedCaseService.list(stringObjectMap);
		ExcelUtil<MenRelatedCaseDO> util = new ExcelUtil<MenRelatedCaseDO>(MenRelatedCaseDO.class);
		return util.exportExcel(list, "menRelatedCase");
	}

	/**
	 * 获取项目案例详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menRelatedCaseService.get(id));
	}

	/**
	 * 新增项目案例
	 */
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:add')")
	@Log(title = "项目案例", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenRelatedCaseDO menRelatedCase)
	{
		menRelatedCase.setId(IdUtil.simpleUUID());
		menRelatedCase.setDelFlag(0);
		menRelatedCase.setCaAt(new Date());
		return toAjax(menRelatedCaseService.save(menRelatedCase));
	}

	/**
	 * 修改项目案例
	 */
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:edit')")
	@Log(title = "项目案例", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenRelatedCaseDO menRelatedCase)
	{
		menRelatedCase.setUpAt(new Date());
		return toAjax(menRelatedCaseService.update(menRelatedCase));
	}

	/**
	 * 删除项目案例
	 */
	@PreAuthorize("@ss.hasPermi('men:menRelatedCase:remove')")
	@Log(title = "项目案例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenRelatedCaseDO menRelatedCase = menRelatedCaseService.get(string);
				menRelatedCase.setDelFlag(1);
				menRelatedCase.setUpAt(new Date());
				menRelatedCaseService.update(menRelatedCase);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menRelatedCaseService.deleteMenRelatedCaseByIds(ids));
	}
}
